package com.atech.ebayadmin.merchandise.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.atech.ebayadmin.common.pager.Pager;
import com.atech.ebayadmin.common.util.BaseDAOHibernate;
import com.atech.ebayadmin.merchandise.dao.IMdUploadLog;
import com.atech.ebayadmin.merchandise.model.MdUploadLog;
import com.atech.ebayadmin.merchandise.vo.MdUploadVO;

public class MdUploadDAO extends BaseDAOHibernate implements IMdUploadLog{
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(MerchandiseDAO.class);
	@Override
	protected Class getObjClass() {
		return MdUploadLog.class;
	}
	public List<MdUploadLog> listUploadLog(MdUploadVO vo, Pager pager) throws Exception{
		try{
			StringBuilder queryCountHql = new StringBuilder(this.queryCountHql(vo));	
			StringBuilder queryListHql = new StringBuilder(this.queryListHql(vo));
			Query queryCount = super.getSession().createQuery(queryCountHql.toString());
			Query queryList = super.getSession().createQuery(queryListHql.toString());
			int i =0;
			if(StringUtils.isNotBlank(vo.getMdTitle())){
				queryCount.setString(i, "%"+vo.getMdTitle().trim()+"%");
				queryList.setString(i, "%"+vo.getMdTitle().trim()+"%");
				i++;
			}
			if(vo.getMdId() != null && vo.getMdId() !=0){
				queryCount.setInteger(i, vo.getMdId());
				queryList.setInteger(i, vo.getMdId());
				i++;
			}
			if(vo.getUserId() != null && vo.getUserId() !=0){
				queryCount.setInteger(i, vo.getUserId());
				queryList.setInteger(i, vo.getUserId());
				i++;
			}
			if(StringUtils.isNotBlank(vo.getEbayAct())){
				queryCount.setString(i,vo.getEbayAct().trim());
				queryList.setString(i, vo.getEbayAct().trim());
				i++;
			}
			Number total = (Number)queryCount.uniqueResult();
			pager.setTotalRows(total.intValue());
			queryList.setFirstResult(pager.getStartRow());
			queryList.setMaxResults(pager.getPageSize());
			return queryList.list();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	private String queryCountHql(MdUploadVO vo){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from ");
		sb.append(getObjClass().getName().toString());
		sb.append(this.createHqlString(vo));
		return sb.toString();
	}
	private String queryListHql(MdUploadVO vo){
		StringBuilder sb = new StringBuilder();
		sb.append("from ");
		sb.append(getObjClass().getName().toString());
		sb.append(this.createHqlString(vo));
		return sb.toString();
	}
	private String createHqlString(MdUploadVO vo){		
		StringBuilder sb = new StringBuilder();
		sb.append(" t where 1=1 ");
		if(StringUtils.isNotBlank(vo.getMdTitle())){
			sb.append(" and t.mdTitle like ?").append("");
		}
		if(vo.getMdId() != null && vo.getMdId() !=0){
			sb.append(" and t.mdId = ?");
		}
		if(vo.getUserId() != null && vo.getUserId() !=0){
			sb.append(" and t.userId = ?");
		}
		if(StringUtils.isNotBlank(vo.getEbayAct())){
			sb.append(" and t.ebayAct = ?");
		}
		sb.append(" order by t.mdLogId desc");
		return sb.toString();
	}
}
